//
//  XMCameraAngleTableViewCell.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/4/22.
//  Copyright © 2018年 smallyou. All rights reserved.
//

//controller
#import "XMCameraController.h"
#import "XMCameraNavigationController.h"

//view
#import "XMCameraAngleTableViewCell.h"
#import "XMCameraAngleCollectionViewCell.h"
#import "KBaseCollectionView.h"

//model
#import "KV2CameraAngleNormalModel.h"

//
#import "UIImage+Rotate.h"


@interface XMCameraAngleTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) KBaseCollectionView * collectionView;


@end

@implementation XMCameraAngleTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


-(void)setUI{
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.equalTo(self);
    }];
}


#pragma mark ----collection----
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XMCameraAngleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[XMCameraAngleCollectionViewCell className] forIndexPath:indexPath];
    
    
    if (indexPath.row < self.data.count) {
        cell.model = self.data[indexPath.row];
        cell.addImageView.hidden = YES;
    }else{
        cell.titleStr = @"添加自定义";
        cell.picImageView.image = nil;
        cell.addImageView.hidden = NO;
    }

    return cell;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.data.count+1;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat  LRMargin = 15;
    
    NSInteger itemW = (kScreenWidth-LRMargin*4)/2;
    
    return CGSizeMake(itemW, KRealValue(190));
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(10,15,10,15);//分别为上、左、下、右
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row >= self.data.count) {
        
        KV2CameraAngleModel * newModel = [[KV2CameraAngleModel alloc]init];
        newModel.name = @"自定义";
        [self.data addObject:newModel];

        [self.collectionView reloadData];
        if (self.addCellBlock) {
            self.addCellBlock();
        }
        return;
    }
        
    KV2CameraAngleModel * model = self.data[indexPath.row];
    
    XMCameraController * vc = [[XMCameraController alloc]init];
    //面部
    if ([model.subSubCateId isEqualToString:@"100000626000008"]) {
        if ([model.name isEqualToString:@"正面"]) {
            vc.angleType = CameraAngleTypeFaceFront;
            
        }else if ([model.name isEqualToString:@"左斜45°"]){
            vc.angleType = CameraAngleTypeFaceLeft45;
            
        }else if ([model.name isEqualToString:@"左侧90°"]){
            vc.angleType = CameraAngleTypeFaceLeft90;
            
        }else if ([model.name isEqualToString:@"右斜45°"]){
            vc.angleType = CameraAngleTypeFaceRight45;
            
        }else if ([model.name isEqualToString:@"右侧90°"]){
            vc.angleType = CameraAngleTypeFaceRight90;
            
        }else if ([model.name isEqualToString:@"仰角"]){
            vc.angleType = CameraAngleTypeFaceTop;
        }else{
            vc.angleType = CameraAngleTypeNone;
        }
        //身体
    }else if ([model.subSubCateId isEqualToString:@"100000626000029"]){
        if ([model.name isEqualToString:@"正面"]) {
            vc.angleType = CameraAngleTypeBodyfront;
            
        }else if ([model.name isEqualToString:@"左斜45°"]){
            vc.angleType = CameraAngleTypeBodyLeft45;
            
        }else if ([model.name isEqualToString:@"左侧90°"]){
            vc.angleType = CameraAngleTypeBodyLeft90;
            
        }else if ([model.name isEqualToString:@"右斜45°"]){
            vc.angleType = CameraAngleTypeBodyRight45;
            
        }else if ([model.name isEqualToString:@"右侧90°"]){
            vc.angleType = CameraAngleTypeBodyRight90;
            
        }else if ([model.name isEqualToString:@"背面"]){
            vc.angleType = CameraAngleTypeBodyBack;
        }else{
            vc.angleType = CameraAngleTypeNone;
        }
    }else if ([model.name isEqualToString:@"自定义"]){
        vc.angleType = CameraAngleTypeNormal;
    }else{
        vc.angleType = CameraAngleTypeNone;
    }
    

    [vc setCallback:^(UIImage *image) {
        
        //合并图片
        UIImage *standImage = [[SDImageCache sharedImageCache] imageFromCacheForKey:model.imageUrl];
        [standImage rotate:UIImageOrientationLeft];

        UIGraphicsBeginImageContext(CGSizeMake(image.size.width + image.size.width, image.size.height));
        [standImage drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
        [image drawInRect:CGRectMake(image.size.width, 0, image.size.width, image.size.height)];
        UIImage *syncImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        model.normalImage = image;
        model.syncImage = syncImage;
        [collectionView reloadData];
    }];
    

    vc.standrandImageUrl = model.imageUrl;
    
    XMCameraNavigationController *nav = [[XMCameraNavigationController alloc] initWithRootViewController:vc];
    
    [self.viewController.navigationController presentViewController:nav animated:YES completion:nil];

    
}



#pragma mark ----getting
//列表
-(KBaseCollectionView *)collectionView{
    
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 30;
        _collectionView = [[KBaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = kWhiteColor;
        _collectionView.scrollEnabled = NO;
        
        [self addSubview:_collectionView];
        
        [_collectionView registerClass:[XMCameraAngleCollectionViewCell class] forCellWithReuseIdentifier:[XMCameraAngleCollectionViewCell className]];
        
    }
    return _collectionView;
}



-(void)setData:(NSMutableArray *)data{
    _data = data;
    [self setUI];
    [self.collectionView reloadData];
}

-(void)dealloc{
    
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
